//
//  HomeViewController.m
//  Ev_Master
//
//  Created by Evan on 2019/6/10.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeBannerTableViewCell.h"
#import "WebViewController.h"
#import "LoginViewController.h"
#import "HomeCenterItemCell.h"
#import "HomeCommodityTableViewCell.h"
#import "taskHomeShop.h"
#import "UIImageView+WebCache.h"
#import "UserCenter.h"
#import "HomeGoCell.h"

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)    UITableView *tableView;

//@property(nonatomic, strong) NSArray<modelHomeShopListInfo*> *arrayDataShop;

@end

@implementation HomeViewController{
    NSArray<modelHomeShopListInfo*> * _arrayDataShop;
    UIImageView *imgTopView;
    NSMutableArray *arrayImg;
    NSMutableArray *arrayImgBg;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"首页";
    self.isNeedLogin = NO;
    ///topView Banner
    arrayImg = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"banner1"],[UIImage imageNamed:@"banner2"],[UIImage imageNamed:@"banner3"], nil];
    arrayImgBg = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"banner1BG"],[UIImage imageNamed:@"banner2BG"],[UIImage imageNamed:@"banner3BG"], nil];
    
    imgTopView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 250)];
    imgTopView.image = [UIImage imageNamed:@"banner2BG"];
    [self.view addSubview:imgTopView];
    
    /// tableview
    _arrayDataShop = [NSArray<modelHomeShopListInfo*> new];
//    [self.tableView reloadData];
//    if ([[UserCenter shareCenter]isLogined]) {
    [self reloadData];
//    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData:) name:Notification_login_ok_notice object:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self hideHeadView:YES];
    [self setHiddenTabBar:NO];
    [self setHiddenNavigation:YES];
    [self setStatusBarBackgroundColor:[UIColor clearColor]];
}


-(void)refreshData:(id)sender{
    [self reloadData];
}

#pragma mark - load Data

- (void)reloadData{
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:@"11" forKey:@"classid"];
    [DKProgressHUD showLoading];
    [taskHomeShop getShopList:dict success:^(modelHomeShopListData * _Nonnull response, BOOL success) {
        [DKProgressHUD dismiss];
        self->_arrayDataShop = [response.data mutableCopy];
        [self.tableView reloadData];
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

#pragma mark - tableview

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -1, KScreenWidth, KScreenHeight - KSHeight_TabBar) style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 4) {
        return _arrayDataShop.count/2 + _arrayDataShop.count%2;
    }
    return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, 0)];
        view.backgroundColor = COLOR_main;
        CGRect rect = [[UIApplication sharedApplication] statusBarFrame];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, rect.size.height + 10, view.frame.size.width - 40, 30)];
        label.backgroundColor = [[UIColor alloc] initWithRed:255/255 green:255/255 blue:255/255 alpha:0.4];
        label.layer.cornerRadius = 15;
        label.layer.masksToBounds = YES;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.text = @"善缘国际区块链应用商城正式上线...";
        [view addSubview:label];
        return view;
    }
    
    UIView *ss = [[UIView alloc]init];
    return ss;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 50;
    }
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return KScreenWidth * 9 / 16;
    }else if (indexPath.section == 1) {
        return 100;
    }
    else if (indexPath.section == 2) {
        return 100;
    }
    else if(indexPath.section == 3){
        return 50;
    }
    
    return 230;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        HomeBannerTableViewCell *cell = [HomeBannerTableViewCell cellWithTableView:tableView IndexPath:indexPath];
        
        cell.imageArray = arrayImg;
        cell.backgroundColor = [UIColor clearColor];
        cell.blockPageNumber = ^(NSInteger pageNumber) {
            UIImage *img = [self->arrayImgBg objectAtIndex:pageNumber];
            self->imgTopView.image = img;
        };
        return cell;
    }
    else if(indexPath.section == 1){
        HomeCenterItemCell *cell = [HomeCenterItemCell cellWithTableView:tableView IndexPath:indexPath];
        cell.blockBtnClick = ^(NSInteger btnIndex) {
            [self cellCenterNavi:btnIndex];
        };
        return cell;
    }
    else if(indexPath.section == 2){
        HomeGoCell *cell = [HomeGoCell cellWithTableView:tableView IndexPath:indexPath];
        return cell;
    }
    else if(indexPath.section == 3){
        
        static NSString * CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [[cell textLabel] setText:@"商城专区"];
        return cell;
    }
    else if(indexPath.section == 4){
        HomeCommodityTableViewCell *cell = [HomeCommodityTableViewCell cellWithTableView:tableView IndexPath:indexPath];
        
        if (_arrayDataShop.count == 0) {
            return cell;
        }
        
        NSInteger indexRow = indexPath.row*2;
        modelHomeShopListInfo *model1 =   [_arrayDataShop  objectAtIndex:indexRow];
        modelHomeShopListInfo *m1 = [modelHomeShopListInfo mj_objectWithKeyValues:model1];
        
//        NSString *baseUlr = baseImgUrl;
        
//        NSString *strUrl = [NSString stringWithFormat:@"%@%@",baseUlr,m1.ProductSwfurl];
        [cell.viewLeftImg sd_setImageWithURL:[NSURL URLWithString:m1.ProductSwfurl]];
        [cell.viewLeftTitle setText:m1.ProductName];
        [cell.viewLeftPrice setText:m1.Price];
        cell.viewLeft.tag = m1.ID;
       
        
        if((indexRow+1) < _arrayDataShop.count){
            modelHomeShopListInfo *model2 = [_arrayDataShop objectAtIndex:(indexRow+1)];
            modelHomeShopListInfo *m2 = [modelHomeShopListInfo mj_objectWithKeyValues:model2];
//            strUrl = [NSString stringWithFormat:@"%@%@",baseUlr,m2.ProductSwfurl];
            [cell.viewRightImg sd_setImageWithURL:[NSURL URLWithString:m2.ProductSwfurl]];
            [cell.viewRightTitle setText:m2.ProductName];
            [cell.viewRightPrice setText:m2.Price];
            cell.viewRight.tag = m2.ID;
        }
        cell.blockTapViewId = ^(NSInteger tagId) {
            NSLog(@"商品ID：%ld",(long)tagId);
            WebViewController *viewController = [[WebViewController alloc]init];
            [viewController cyl_setNavigationBarHidden:NO];
            viewController.webUrl = [NSString stringWithFormat:@"%@/exdetails/%ld",WebViewH5Host,(long)tagId];
            [self.navigationController  pushViewController:viewController animated:YES];
        };
        
        return cell;
    }
    return [UITableViewCell new];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 2) {
        self.tabBarController.selectedIndex = 1;
    }
    //    LoginViewController *vc = [[LoginViewController alloc]init];
    //    [self.navigationController showViewController:vc sender:nil];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


#pragma mark - 中间按钮组点击事件

-(void)cellCenterNavi:(NSInteger)btnIndex{
    NSString *strUrl = @"";
    switch (btnIndex) {
        case 1://奖励账户
            strUrl = @"/user/account1";
            break;
        case 2://体验账户
            strUrl = @"/user/account2";
            break;
        case 3://静态账户
            strUrl = @"/user/account3";
            break;
        case 4://动态账户
            strUrl = @"/user/account4";
            break;
        case 5://数字资产
            strUrl = @"/assets";
            break;
    }
    if(strUrl.length>0)
    {
        WebViewController *viewController = [[WebViewController alloc]init];
        [viewController cyl_setNavigationBarHidden:NO];
        viewController.webUrl = [NSString stringWithFormat:@"%@%@",WebViewH5Host,strUrl];
        [self.navigationController  pushViewController:viewController animated:YES];
    }
}


@end
